﻿using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Indt.Components
{
    public class SliderView
    {
        private Grid root;
        private ScrollViewer container;
        private StackPanel content;
        public int index {get; set;}
        private int itemCount;
        private double value;

        private int _width;

        public SliderView(Grid target, int width)
        {
            Debug.WriteLine("Slider View Constructor");
            root = target;
            _width = width;
            index = 0;

            StackPanel();
        } 

        private void Flick(object sender, FlickGestureEventArgs e)
        {
            Debug.WriteLine("Flick");

            if (e.Direction.ToString() == "Horizontal" && itemCount > 0)
            {
                if (e.HorizontalVelocity > 0) // Right
                {
                    if (index > 0)
                    {
                        --index;
                        container.LayoutUpdated += new EventHandler(LayoutUpdated);
                        Debug.WriteLine("Flick Right " + index);
                    }
                }
                else //Left
                {
                    if (index < itemCount - 1)
                    {
                        ++index;
                        container.LayoutUpdated += new EventHandler(LayoutUpdated);
                        Debug.WriteLine("Flick Left " + index);
                    }
                }
            }
        }

        private void LayoutUpdated(object sender, EventArgs e)
        {
            value = index * _width;
            container.ScrollToHorizontalOffset(value);
            container.LayoutUpdated -= new EventHandler(LayoutUpdated);
            Debug.WriteLine("LayoutUpdated " + value);
        }

        private void ScrollViewer() 
        {
            Debug.WriteLine("Container");
            container = new ScrollViewer();
            container.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
            container.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
            container.Content = content;

            root.Children.Add(container);

            Debug.WriteLine("Listener");
            GestureListener GestureListener = GestureService.GetGestureListener(container);
            GestureListener.Flick += new EventHandler<FlickGestureEventArgs>(Flick);
        }

        private void StackPanel()
        {
            Debug.WriteLine("Content");
            content = new StackPanel();
            content.Orientation = Orientation.Horizontal;
            content.Margin = new Thickness(0,0,0,0);

            ScrollViewer();
        }

        public void AddItem(UIElement item) 
        {
            content.Children.Add(item);
            itemCount = content.Children.Count;
            Debug.WriteLine("Add Item " + itemCount);
        }
    }
}
